/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     graphicsNumber.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "graphicsNumber.h"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "bsp.h"
#include "popupBox.h"
#include "levetop.h"
#include "transport.h"

/***********************************************************************************
* Function Name : pic_date_display
* Description   : Display data in picture mode.
* Input         : - type: data info sources
				  - buff: data info
* Output        : None
* Return        : None
************************************************************************************/
void pic_date_display(uint8_t type, uint8_t *buff)
{
	uint16_t p_add, pointer;
	uint16_t x, y, xd;
	uint16_t simage, eimage;
	uint8_t data_type, I_num, D_num, Alig, i = 0, num = 0;
	uint8_t flag = 0, nflag = 0;
	uint16_t num_w,num_h, dot_w, neg_w,dot_h, neg_h;
	int8_t value0 = 0;
	int16_t value1 = 0;
	int32_t value2 = 0;
	uint32_t value3 = 0;
	int64_t value = 0;
	uint16_t tft_x, buf_x,tft_y,buf_y;
	uint8_t buff1[12] = {0};
	uint8_t leading_zero = 0;

	uint16_t w_real = 0, h_real = 0;
    uint16_t w_max = 0, h_max = 0, w0 = 0, h0 = 0;

	if (type == 0)		// Instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8)) * 2;
		data_type = buff[7];
		x = buff[8] + (buff[9] << 8);
		y = buff[10] + (buff[11] << 8);
		I_num = buff[12];
		D_num = buff[13];
		Alig = buff[14] & 0x7F;
		leading_zero = (buff[14] & 0x80) >> 7;
		simage = buff[15] + (buff[16] << 8);
		eimage = buff[17] + (buff[18] << 8);
	}
	else if (type == 1) // Instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8)) * 2;
		data_type = var[pointer + 3];
		x = var[pointer + 4] + (var[pointer + 5] << 8);
		y = var[pointer + 6] + (var[pointer + 7] << 8);
		I_num = var[pointer + 8];
		D_num = var[pointer + 9];
		Alig = var[pointer + 10] & 0x7F;
		leading_zero = (var[pointer + 10] & 0x80) >> 7;
		simage = var[pointer + 11] + (var[pointer + 12] << 8);
		eimage = var[pointer + 13] + (var[pointer + 14] << 8);
	}

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	if(download_flag == 3)
	{
		if (Screen_DIR == 0 || Screen_DIR == 2)
			x += LCD_XSIZE_TFT * 2;
		else if (Screen_DIR == 1 || Screen_DIR == 3)
			y += LCD_YSIZE_TFT * 2;
	}

	if (data_type == 0x00)
	{
		value3 = var[p_add + 1];
		value = value3;
	}
	else if (data_type == 0x01)
	{
		value3 = (var[p_add] << 8) + var[p_add + 1];
		value = value3;
	}
	else if (data_type == 0x02)
	{
		value3 = (0xFF000000 & (var[p_add] << 24)) | (var[p_add + 1] << 16) | (var[p_add + 2] << 8) | var[p_add + 3];
		value = value3;
	}
	else if (data_type == 0x80)
	{
		value0 = var[p_add + 1];
		value = value0;
	}
	else if (data_type == 0x81)
	{
		value1 = (var[p_add] << 8) + var[p_add + 1];
		value = value1;
	}
	else if (data_type == 0x82)
	{
		value2 = (var[p_add] << 24) | (var[p_add + 1] << 16) | (var[p_add + 2] << 8) | var[p_add + 3];
		value = value2;
	}
	else if (data_type == 0x83)
	{
		value = (0xFF000000 & (var[p_add] << 24)) + (var[p_add + 1] << 16) + (var[p_add + 2] << 8) + var[p_add + 3];
		value = ((value << 24) << 8) + (0xFF000000 & (var[p_add + 4] << 24)) + (var[p_add + 5] << 16) + (var[p_add + 6] << 8) + var[p_add + 7];
	}

	memset(Display_buff, 0, 50);

	if (value < 0)
	{
		Display_buff[num] = 11; // negative number
		value = value * (-1);	// Become an integer
		nflag = 1;
		num++;
	}

	if (value < 1000000000000000000)
		value = value % ((int64_t)LtPow(10, I_num + D_num));

	for (i = 0; i < I_num; i++) // Integer part
	{
		Display_buff[num] = value / (int64_t)LtPow(10, I_num - i - 1 + D_num);
		if (leading_zero == 0)
		{
			if (Display_buff[num] != 0 || flag == 1)
			{
				num++;
				flag = 1; // The first digit is not 0
			}
		}
		else
		{
			num++;
			flag = 1; // The first digit is not 0
		}
		value = value % (int64_t)LtPow(10, I_num - i - 1 + D_num);
	}

	if (flag == 0)
	{
		Display_buff[num] = 0;
		num++;
	}
	if (D_num > 0)
	{
		Display_buff[num] = 10; //"."
		num++;
	}

	for (i = 0; i < D_num; i++) // Fractional part
	{
		Display_buff[num] = value / (int64_t)LtPow(10, D_num - i - 1);
		num++;
		value = value % (int64_t)LtPow(10, D_num - i - 1);
	}

	LT_ReadFlash_UI(buff1, addr_index[10] + 12 * simage + multi_language * 12 * addr_index[11], 12);
	pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
	num_w = buff1[4] + (buff1[5] << 8);
	w0 = buff1[4] + (buff1[5] << 8);
	h0 = buff1[6] + (buff1[7] << 8);
	pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
	pic_flag = buff1[11];

	PICINFO[0].w = num_w;
	PICINFO[0].h = h0;
	PICINFO[0].len = pic_len;
	PICINFO[0].flag = pic_flag;
	PICINFO[0].addr = pic_add;

	if (D_num > 0) // have point
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + 10) + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		dot_w = buff1[4] + (buff1[5] << 8);
		dot_h = buff1[6] + (buff1[7] << 8);
		PICINFO[1].w = dot_w;
		PICINFO[1].addr = pic_add;
	}

	if (nflag == 1) // have minus
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + 11) + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		neg_w = buff1[4] + (buff1[5] << 8);
		neg_h = buff1[6] + (buff1[7] << 8);
		PICINFO[2].w = neg_w;
		PICINFO[2].addr = pic_add;
	}

	xd = 0;
	i = 0;
	for (i = 0; i < num; i++) // display part
	{
		if (Display_buff[i] >= 0 && Display_buff[i] <= 9)
		{
			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + Display_buff[i]) + multi_language * 12 * addr_index[11], 8);
			num_w = buff1[4] + (buff1[5] << 8);
			num_h = buff1[6] + (buff1[7] << 8);
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += num_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += num_h;
		}
		else if (Display_buff[i] == 10)
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += dot_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += dot_h;
//			xd += dot_w;
		}
		else if (Display_buff[i] == 11)
		{
//			xd += neg_w;
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += neg_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += neg_h;
		}
	}

	if(xd%2) xd++;

	if (pdata_D_w[pdata_D_num] > xd) // The previous data is wider than the current data, and more content needs to be cleared
	{
		if (Screen_DIR == 0)
		{
			w_max = pdata_D_w[pdata_D_num];
			h_max = h0;
			tft_y = y;
			buf_y = 0;
			if (Alig == 0) // left
			{
				tft_x = x;
				buf_x = 0;
			}
			else if (Alig == 1) // center
			{
				tft_x = x - w_max / 2;
				buf_x = (w_max - xd) / 2;
			}
			else if (Alig == 2) // right
			{
				tft_x = x - w_max + 1;
				buf_x = w_max - xd;
			}
		}
		else if (Screen_DIR == 1)
		{
			w_max = w0;
			h_max = pdata_D_w[pdata_D_num];
			if (Alig == 0) // left
			{
				tft_x = x - w0;
				tft_y = y;
				buf_x = 0;
				buf_y = 0;
			}
			else if (Alig == 1) // center
			{
				tft_x = x - w0;
				tft_y = y - h_max / 2;;
				buf_x = 0;
				buf_y = (h_max - xd)/2;
			}
			else if (Alig == 2) // right
			{
				tft_x = x - w0;
				tft_y = y - h_max;;
				buf_x = 0;
				buf_y = h_max - xd;
			}
		}
		else if (Screen_DIR == 2)
		{
			w_max = pdata_D_w[pdata_D_num];
			h_max = h0;
			if (Alig == 0) // left
			{
				tft_x = x - w_max;
				tft_y = y - h0;
				buf_x = xd - w0 + w_max - xd;
				buf_y = 0;
			}
			else if (Alig == 1) // center
			{
				tft_x = x - w_max/2;
				tft_y = y - h0;
				buf_x = xd - w0 + (w_max - xd)/2;
				buf_y = 0;
			}
			else if (Alig == 2) // right
			{
				tft_x = x ;
				tft_y = y - h0;
				buf_x = xd - w0;
				buf_y = 0;
			}
		}
		else if (Screen_DIR == 3)
		{
			w_max = w0;
			h_max = pdata_D_w[pdata_D_num];
			if (Alig == 0) // left
			{
				tft_x = x;
				tft_y = y - h_max;
				buf_x = 0;
				buf_y = xd  + h_max - xd;
			}
			else if (Alig == 1) // center
			{
				tft_x = x;
				tft_y = y - h_max / 2;
				buf_x = 0;
				buf_y = xd  + (h_max - xd)/2;
			}
			else if (Alig == 2) // right
			{
				tft_x = x;
				tft_y = y;
				buf_x = 0;
				buf_y = xd ;
			}
		}
	}
	else
	{
		if (Screen_DIR == 0)
		{
			w_max = xd;
			h_max = h0;
			buf_x = 0;
			tft_y = y;
			buf_y = 0;
			if (Alig == 0)
				tft_x = x;
			else if (Alig == 1)
				tft_x = x - w_max / 2;
			else if (Alig == 2)
				tft_x = x - w_max + 1;
		}
		else if (Screen_DIR == 1)
		{
			w_max = w0;
			h_max = xd;
			if (Alig == 0) // left
			{
				tft_x = x - w0;
				tft_y = y;
				buf_x = 0;
				buf_y = 0;
			}
			else if (Alig == 1) // center
			{
				tft_x = x - w0;
				tft_y = y - h_max / 2;
				buf_x = 0;
				buf_y = 0;
			}
			else if (Alig == 2) // right
			{
				tft_x = x - w0;
				tft_y = y - h_max;
				buf_x = 0;
				buf_y = 0;
			}
		}
		else if (Screen_DIR == 2)
		{
			w_max = xd;
			h_max = h0;
			if (Alig == 0) // left
			{
				tft_x = x - xd;
				tft_y = y - h0;
				buf_x = xd - w0;
				buf_y = 0;
			}
			else if (Alig == 1) // center
			{
				tft_x = x - xd/2;
				tft_y = y - h0;
				buf_x = xd - w0;
				buf_y = 0;
			}
			else if (Alig == 2) // right
			{
				tft_x = x ;
				tft_y = y - h0;
				buf_x = xd - w0;
				buf_y = 0;
			}
		}
		else if (Screen_DIR == 3)
		{
			w_max = w0;
			h_max = xd;
			if (Alig == 0) // left
			{
				tft_x = x;
				tft_y = y - h_max;
				buf_x = 0;
				buf_y = xd;
			}
			else if (Alig == 1) // center
			{
				tft_x = x;
				tft_y = y - h_max / 2;
				buf_x = 0;
				buf_y = xd;
			}
			else if (Alig == 2) // right
			{
				tft_x = x;
				tft_y = y;//- h_max;
				buf_x = 0;
				buf_y = xd;
			}
		}


	}
	if(pic_update == 0)
	{
		LT268_BG_to_buffer(0, 0, w_max, m_PngData,
						   tft_x, tft_y,w_max, h_max);
	}
	else
	{
		buf_x = buf_x + tft_x;
		buf_y = buf_y + tft_y;
		w_max = FullScreen_w;
	}


	for (i = 0; i < num; i++) // display
	{
		if (Display_buff[i] >= 0 && Display_buff[i] <= 9)
		{

			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + Display_buff[i]) + multi_language * 12 * addr_index[11], 8);
			pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
			num_w = buff1[4] + (buff1[5] << 8);
			num_h = buff1[6] + (buff1[7] << 8);

			if (Screen_DIR == 0 || Screen_DIR == 1)
			{
				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, num_w, pic_add,
									num_w, num_h, pic_flag);
				if (Screen_DIR == 0)
					buf_x += num_w;
				else if (Screen_DIR == 1)
					buf_y += num_h;
			}
			else if (Screen_DIR == 2 || Screen_DIR == 3)
			{
				if (Screen_DIR == 2)
					buf_x -= num_w;
				else if (Screen_DIR == 3)
					buf_y -= num_h;
				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, num_w, pic_add,
									num_w, num_h, pic_flag);
			}



		}
		else if (Display_buff[i] == 10)
		{
			if (Screen_DIR == 0 || Screen_DIR == 1)
			{
				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, PICINFO[1].w, PICINFO[1].addr,
									PICINFO[1].w, dot_h, pic_flag);
				if (Screen_DIR == 0)
					buf_x += dot_w;
				else if (Screen_DIR == 1)
					buf_y += dot_h;
			}
			else if (Screen_DIR == 2 || Screen_DIR == 3)
			{
				if (Screen_DIR == 2)
					buf_x -= dot_w;
				else if (Screen_DIR == 3)
					buf_y -= dot_h;

				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, PICINFO[1].w, PICINFO[1].addr,
									PICINFO[1].w, dot_h, pic_flag);
			}


		}
		else if (Display_buff[i] == 11)
		{
			if (Screen_DIR == 0 || Screen_DIR == 1)
			{
				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, PICINFO[2].w, PICINFO[2].addr,
									PICINFO[2].w, neg_h, pic_flag);
				if (Screen_DIR == 0)
					buf_x += neg_w;
				else if (Screen_DIR == 1)
					buf_y += neg_h;
			}
			else if (Screen_DIR == 2 || Screen_DIR == 3)
			{
				if (Screen_DIR == 2)
					buf_x -= neg_w;
				else if (Screen_DIR == 3)
					buf_y -= neg_h;
				LT268_pic_to_buffer(buf_x, buf_y, w_max, m_PngData,
									0, 0, PICINFO[2].w, PICINFO[2].addr,
									PICINFO[2].w, neg_h, pic_flag);
			}

		}
	}

	if(pic_update == 0)
	{
		LT268_buffer_to_TFT(0, 0, w_max, m_PngData,
							tft_x, tft_y, w_max, h_max);
	}

	pdata_D_w[pdata_D_num] = xd; // Record width

}


void pic_date_display_to_buff(uint8_t type, uint8_t *buff,uint16_t overlay_x ,uint16_t overlay_y,uint16_t overlay_w)
{
	uint16_t p_add, pointer;
	uint16_t x, y, xd, w0 = 0, h0 = 0;;
	uint16_t simage, eimage;
	uint8_t data_type, I_num, D_num, Alig, i = 0, num = 0;
	uint8_t flag = 0, nflag = 0;
	uint16_t num_w,num_h, dot_w, neg_w,dot_h, neg_h;
	int8_t value0 = 0;
	int16_t value1 = 0;
	int32_t value2 = 0;
	uint32_t value3 = 0;
	int64_t value = 0;
	uint16_t tft_x, buf_x,tft_y,buf_y;
	uint8_t buff1[12] = {0};
	uint8_t leading_zero = 0;


	if (type == 0)		// Instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8)) * 2;
		data_type = buff[7];
		x = buff[8] + (buff[9] << 8);
		y = buff[10] + (buff[11] << 8);
		I_num = buff[12];
		D_num = buff[13];
		Alig = buff[14] & 0x7F;
		leading_zero = (buff[14] & 0x80) >> 7;
		simage = buff[15] + (buff[16] << 8);
		eimage = buff[17] + (buff[18] << 8);
	}
	else if (type == 1) // Instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8)) * 2;
		data_type = var[pointer + 3];
		x = var[pointer + 4] + (var[pointer + 5] << 8);
		y = var[pointer + 6] + (var[pointer + 7] << 8);
		I_num = var[pointer + 8];
		D_num = var[pointer + 9];
		Alig = var[pointer + 10] & 0x7F;
		leading_zero = (var[pointer + 10] & 0x80) >> 7;
		simage = var[pointer + 11] + (var[pointer + 12] << 8);
		eimage = var[pointer + 13] + (var[pointer + 14] << 8);
	}

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	if (data_type == 0x00)
	{
		value3 = var[p_add + 1];
		value = value3;
	}
	else if (data_type == 0x01)
	{
		value3 = (var[p_add] << 8) + var[p_add + 1];
		value = value3;
	}
	else if (data_type == 0x02)
	{
		value3 = (0xFF000000 & (var[p_add] << 24)) | (var[p_add + 1] << 16) | (var[p_add + 2] << 8) | var[p_add + 3];
		value = value3;
	}
	else if (data_type == 0x80)
	{
		value0 = var[p_add + 1];
		value = value0;
	}
	else if (data_type == 0x81)
	{
		value1 = (var[p_add] << 8) + var[p_add + 1];
		value = value1;
	}
	else if (data_type == 0x82)
	{
		value2 = (var[p_add] << 24) | (var[p_add + 1] << 16) | (var[p_add + 2] << 8) | var[p_add + 3];
		value = value2;
	}
	else if (data_type == 0x83)
	{
		value = (0xFF000000 & (var[p_add] << 24)) + (var[p_add + 1] << 16) + (var[p_add + 2] << 8) + var[p_add + 3];
		value = ((value << 24) << 8) + (0xFF000000 & (var[p_add + 4] << 24)) + (var[p_add + 5] << 16) + (var[p_add + 6] << 8) + var[p_add + 7];
	}

	memset(Display_buff, 0, 50);

	if (value < 0)
	{
		Display_buff[num] = 11; // negative number
		value = value * (-1);	// Become an integer
		nflag = 1;
		num++;
	}

	if (value < 1000000000000000000)
		value = value % ((int64_t)LtPow(10, I_num + D_num));

	for (i = 0; i < I_num; i++) // Integer part
	{
		Display_buff[num] = value / (int64_t)LtPow(10, I_num - i - 1 + D_num);
		if (leading_zero == 0)
		{
			if (Display_buff[num] != 0 || flag == 1)
			{
				num++;
				flag = 1; // The first digit is not 0
			}
		}
		else
		{
			num++;
			flag = 1; // The first digit is not 0
		}
		value = value % (int64_t)LtPow(10, I_num - i - 1 + D_num);
	}

	if (flag == 0)
	{
		Display_buff[num] = 0;
		num++;
	}
	if (D_num > 0)
	{
		Display_buff[num] = 10; //"."
		num++;
	}

	for (i = 0; i < D_num; i++) // Fractional part
	{
		Display_buff[num] = value / (int64_t)LtPow(10, D_num - i - 1);
		num++;
		value = value % (int64_t)LtPow(10, D_num - i - 1);
	}

	LT_ReadFlash_UI(buff1, addr_index[10] + 12 * simage + multi_language * 12 * addr_index[11], 12);
	pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
	num_w = buff1[4] + (buff1[5] << 8);
	w0 = buff1[4] + (buff1[5] << 8);
	h0 = buff1[6] + (buff1[7] << 8);
	pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
	pic_flag = buff1[11];

	PICINFO[0].w = num_w;
	PICINFO[0].h = h0;
	PICINFO[0].len = pic_len;
	PICINFO[0].flag = pic_flag;
	PICINFO[0].addr = pic_add;

	if (D_num > 0) // have point
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + 10) + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		dot_w = buff1[4] + (buff1[5] << 8);
		dot_h = buff1[6] + (buff1[7] << 8);
		PICINFO[1].w = dot_w;
		PICINFO[1].addr = pic_add;
	}

	if (nflag == 1) // have minus
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + 11) + multi_language * 12 * addr_index[11], 12);
		pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		neg_w = buff1[4] + (buff1[5] << 8);
		neg_h = buff1[6] + (buff1[7] << 8);
		PICINFO[2].w = neg_w;
		PICINFO[2].addr = pic_add;
	}

	xd = 0;
	i = 0;
	for (i = 0; i < num; i++) // display part
	{
		if (Display_buff[i] >= 0 && Display_buff[i] <= 9)
		{
			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + Display_buff[i]) + multi_language * 12 * addr_index[11], 8);
			num_w = buff1[4] + (buff1[5] << 8);
			num_h = buff1[6] + (buff1[7] << 8);
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += num_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += num_h;
		}
		else if (Display_buff[i] == 10)
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += dot_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += dot_h;
//			xd += dot_w;
		}
		else if (Display_buff[i] == 11)
		{
//			xd += neg_w;
			if (Screen_DIR == 0 || Screen_DIR == 2)
				xd += neg_w;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				xd += neg_h;
		}
	}

	if(xd%2) xd++;


	if (Screen_DIR == 0)
	{
		tft_y = y;
		if (Alig == 0)
			tft_x = x;
		else if (Alig == 1)
			tft_x = x - xd / 2;
		else if (Alig == 2)
			tft_x = x - xd + 1;
	}
	else if (Screen_DIR == 1)
	{
		tft_x = x - w0;
		if (Alig == 0) // left
			tft_y = y;
		else if (Alig == 1) // center
			tft_y = y - xd / 2;
		else if (Alig == 2) // right
			tft_y = y - xd;
	}
	else if (Screen_DIR == 2)
	{
		tft_y = y - h0;
		if (Alig == 0) // left
			// tft_x = x - xd;
			tft_x = x ;
		else if (Alig == 1) // center
			// tft_x = x - xd/2;
			tft_x = x + xd/2 ;
		else if (Alig == 2) // right
			// tft_x = x ;
			tft_x = x + xd ;
	}
	else if (Screen_DIR == 3)
	{
		tft_x = x;
		if (Alig == 0) // left
			// tft_y = y - xd;
			tft_y = y ;
		else if (Alig == 1) // center
			// tft_y = y - xd / 2;
			tft_y = y + xd / 2 ;
		else if (Alig == 2) // right
			// tft_y = y;
			tft_y = y + xd ;
	}


	if (pic_update == 0)
	{
		buf_x  = tft_x - overlay_x;
		buf_y  = tft_y - overlay_y;
	}
	else
	{
		buf_x  = tft_x;
		buf_y  = tft_y;
		if(download_flag == 3)
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
				buf_x += LCD_XSIZE_TFT * 2;
			else if (Screen_DIR == 1 || Screen_DIR == 3)
				buf_y += LCD_YSIZE_TFT * 2;
		}
	}

	i = 0;
	for (i = 0; i < num; i++) // display
	{
		if (Display_buff[i] >= 0 && Display_buff[i] <= 9)
		{
			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * (simage + Display_buff[i]) + multi_language * 12 * addr_index[11], 8);
			pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
			num_w = buff1[4] + (buff1[5] << 8);
			num_h = buff1[6] + (buff1[7] << 8);

			if(Screen_DIR == 0 || Screen_DIR == 1)
			{
				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, num_w, pic_add,
									num_w, num_h, pic_flag);
				if (Screen_DIR == 0)
					buf_x += num_w;
				else if (Screen_DIR == 1)
					buf_y += num_h;
			}
			else
			{
				if (Screen_DIR == 2)
					buf_x -= num_w;
				else if (Screen_DIR == 3)
					buf_y -= num_h;

				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, num_w, pic_add,
									num_w, num_h, pic_flag);

			}


		}
		else if (Display_buff[i] == 10)
		{
			if(Screen_DIR == 0 || Screen_DIR == 1)
			{
				if (Screen_DIR == 0)
					buf_x += dot_w;
				else if (Screen_DIR == 1)
					buf_y += dot_h;

				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, PICINFO[1].w, PICINFO[1].addr,
									PICINFO[1].w, dot_h, pic_flag);
			}
			else
			{
				if (Screen_DIR == 2)
					buf_x -= dot_w;
				else if (Screen_DIR == 3)
					buf_y -= dot_h;

				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, PICINFO[1].w, PICINFO[1].addr,
									PICINFO[1].w, dot_h, pic_flag);
			}

		}
		else if (Display_buff[i] == 11)
		{
			if(Screen_DIR == 0 || Screen_DIR == 1)
			{
				if (Screen_DIR == 0)
					buf_x += neg_w;
				else if (Screen_DIR == 1)
					buf_y += neg_h;
				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, PICINFO[2].w, PICINFO[2].addr,
									PICINFO[2].w, neg_h, pic_flag);
			}
			else
			{
				if (Screen_DIR == 2)
					buf_x -= neg_w;
				else if (Screen_DIR == 3)
					buf_y -= neg_h;

				LT268_pic_to_buffer(buf_x, buf_y, overlay_w, m_PngData,
									0, 0, PICINFO[2].w, PICINFO[2].addr,
									PICINFO[2].w, neg_h, pic_flag);

			}
		}
	}

}

